#include <iostream>
#include <vector>
#include <fstream>
#include <cctype> // 用于 tolower()
#include "SmartCar.h"
#include "Student.h"
#include "AGXKit.h"
#include "Camera.h"
#include "Lidar.h"
#include "Gyroscope.h"
#include "Display.h"
#include "Battery.h"

using namespace std;

// 显示菜单
void showMenu() {
    cout << "====================" << endl;
    cout << "1. 数据录入" << endl;
    cout << "2. 数据展示" << endl;
    cout << "3. 退出程序" << endl;
    cout << "====================" << endl;
}

// 数据录入函数
void inputData(vector<Student>& students, vector<SmartCar>& cars, vector<AGXKit>& agxKits, vector<Camera>& cameras, vector<Lidar>& lidars, vector<Gyroscope>& gyroscopes, vector<Display>& displays, vector<Battery>& batteries) {
    students.clear();
    cars.clear();
    agxKits.clear();
    cameras.clear();
    lidars.clear();
    gyroscopes.clear();
    displays.clear();
    batteries.clear();

    srand(time(0)); // 初始化随机数种子

    for (int i = 0; i < 10; ++i) {
        Student student;
        SmartCar car;
        AGXKit agx;
        Camera camera;
        Lidar lidar;
        Gyroscope gyroscope;
        Display display;
        Battery battery;

        cout << "录入第 " << (i + 1) << " 名学生的信息：" << endl;
        student.inputStudent();

        car.generateCarID();
        car.generateChassisID();

        students.push_back(student);
        cars.push_back(car);
        agxKits.push_back(agx);
        cameras.push_back(camera);
        lidars.push_back(lidar);
        gyroscopes.push_back(gyroscope);
        displays.push_back(display);
        batteries.push_back(battery);

        cout << "--------------------------" << endl;
    }

    // 保存到文件
    ofstream outFile("car_data.txt");
    if (!outFile) {
        cerr << "无法打开文件保存数据" << endl;
        return;
    }

    for (int i = 0; i < 10; ++i) {
        students[i].save(outFile);
        cars[i].save(outFile);
        agxKits[i].save(outFile);
        cameras[i].save(outFile);
        lidars[i].save(outFile);
        gyroscopes[i].save(outFile);
        displays[i].save(outFile);
        batteries[i].save(outFile);
        outFile << "--------------------------" << endl;
    }

    outFile.close();
    cout << "数据已保存到 car_data.txt 文件。" << endl;
}

// 数据展示函数
// 数据展示函数
void displayData() {
    ifstream inFile("car_data.txt");
    if (!inFile) {
        cerr << "无法打开文件读取数据" << endl;
        return;
    }

    string line;
    vector<string> lines;

    // 读取文件内容
    while (getline(inFile, line)) {
        lines.push_back(line);
    }

    inFile.close();

    // 计算记录数，每个记录有固定的行数（根据类中的打印数据确定）
    int recordSize = 2 + 1 + 7 + 6 + 6 + 2 + 3; // 学生信息2行 + 小车信息7行 + AGX信息6行 + 摄像头信息6行 + 激光雷达信息4行 + 陀螺仪信息2行 + 显示屏信息2行 + 电池信息3行 + 分隔符
    int recordCount = lines.size() / recordSize;

    if (recordCount == 0) {
        cout << "没有数据可展示。" << endl;
        return;
    }

    int index = 0;

    while (true) {
        // 打印一条完整的记录
        int startLine = index * recordSize;

        // 打印学生信息
        cout << "学生学号: " << lines[startLine] << endl;       // 学号
        cout << "学生姓名: " << lines[startLine + 1] << endl;    // 姓名

        // 打印车辆信息
        cout << "车辆编号: " << lines[startLine + 2] << endl; // 车辆编号
        cout << "底盘编号: " << lines[startLine + 3] << endl; // 底盘编号
        cout << "型号: " << lines[startLine + 4] << endl;  // 型号
        cout << "轴距: " << lines[startLine + 5] << " mm" << endl;  // 轴距
        cout << "轮距: " << lines[startLine + 6] << " mm" << endl;   // 轮距
        cout << "最小离地间隙: " << lines[startLine + 7] << " mm" << endl; // 离地间隙
        cout << "最小转弯半径: " << lines[startLine + 8] << " m" << endl; // 最小转弯半径
        cout << "驱动形式: " << lines[startLine + 9] << endl; // 驱动形式
        cout << "最大行程: " << lines[startLine + 10] << " KM" << endl; // 最大行程
        cout << "轮胎型号: " << lines[startLine + 11] << endl; // 轮胎型号
        cout << "轮胎尺寸: " << lines[startLine + 12] << " mm" << endl; // 轮胎尺寸

        // 打印 AGX 套件信息
        cout << "AGX 套件型号: " << lines[startLine + 13] << endl; // AGX 套件型号
        cout << "AI TOPS: " << lines[startLine + 14] << endl; // AI TOPS
        cout << "CUDA 核心: " << lines[startLine + 15] << endl; // CUDA 核心
        cout << "Tensor 核心: " << lines[startLine + 16] << endl; // Tensor 核心
        cout << "显存: " << lines[startLine + 17] << " GB" << endl; // 显存
        cout << "存储: " << lines[startLine + 18] << " GB" << endl; // 存储

        // 打印摄像头信息
        cout << "摄像头型号: " << lines[startLine + 19] << endl; // 摄像头型号
        cout << "传感器型号: " << lines[startLine + 20] << endl; // 传感器型号
        cout << "RGB 分辨率: " << lines[startLine + 21] << endl; // RGB 分辨率
        cout << "RGB 帧率: " << lines[startLine + 22] << " FPS" << endl; // RGB 帧率
        cout << "FOV: " << lines[startLine + 23] << endl; // FOV
        cout << "深度帧率: " << lines[startLine + 24] << " FPS" << endl; // 深度帧率

        // 打印激光雷达信息
        cout << "激光雷达型号: " << lines[startLine + 25] << endl; // 激光雷达型号
        cout << "通道数: " << lines[startLine + 26] << endl; // 通道数
        cout << "测试范围: " << lines[startLine + 27] << " m" << endl; // 测试范围
        cout << "功耗: " << lines[startLine + 28] << " W" << endl; // 功耗

        // 打印陀螺仪信息
        cout << "陀螺仪型号: " << lines[startLine + 29] << endl; // 陀螺仪型号
        cout << "厂家: " << lines[startLine + 30] << endl; // 厂家

        // 打印显示屏信息
        cout << "液晶显示屏尺寸: " << lines[startLine + 31] << " 英寸" << endl; // 尺寸
        cout << "型号: " << lines[startLine + 32] << endl; // 型号

        // 打印电池信息
        cout << "电池参数: " << lines[startLine + 33] << endl; // 电池参数
        cout << "对外供电: " << lines[startLine + 34] << endl; // 对外供电
        cout << "充电时长: " << lines[startLine + 35] << " 小时" << endl; // 充电时长

        // 分隔符
        cout << "--------------------------" << endl;

        // 用户输入控制
        char command;
        cout << "按n显示下一辆，按p显示上一辆，按q退出: ";
        cin >> command;
        command = tolower(command);  // 将输入转换为小写

        if (command == 'n') {
            if (index < (recordCount - 1)) {
                index++;
            } else {
                cout << "已经是最后一辆车了。" << endl;
            }
        }
        else if (command == 'p') {
            if (index > 0) {
                index--;
            } else {
                cout << "已经是第一辆车了。" << endl;
            }
        }
        else if (command == 'q') {
            break;
        }

        cout << endl;
    }
}


int main() {
    vector<Student> students;
    vector<SmartCar> cars;
    vector<AGXKit> agxKits;
    vector<Camera> cameras;
    vector<Lidar> lidars;
    vector<Gyroscope> gyroscopes;
    vector<Display> displays;
    vector<Battery> batteries;
    int choice;

    while (true) {
        showMenu();
        cout << "请选择操作: ";
        cin >> choice;

        switch (choice) {
            case 1:
                inputData(students, cars, agxKits, cameras, lidars, gyroscopes, displays, batteries);
                break;
            case 2:
                displayData();
                break;
            case 3:
                cout << "退出程序" << endl;
                return 0;
            default:
                cout << "无效的选择，请重新输入。" << endl;
                break;
        }
    }

    return 0;
}
